package com.mgm.test;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
public class ExtractMessage {
 
	public static void main(String[] args) {
		
//		String msg = "PerformanceManager[第1个中括号]Product[第2个中括号]<[第3个中括号]79~";
		String msg = "https://apptrknow.com/dir/redirect?placement_id=10219&campaign_id=29013123&affid=8732&cid=[cid]&data1=[data1]&data2=[data2]&data3=[data3]&data4=[data4]&affsub1=[affsub1]&device_id=[device_id]&idfa=[idfa]&gaid=[gaid]&source_app=[source_app]";
		List<String> list = extractMessageByRegular(msg);
		for (int i = 0; i < list.size(); i++) {
			System.out.println(i+"-->"+list.get(i));
		}
	}
	
	/**
	 * 使用正则表达式提取中括号中的内容
	 * @param msg
	 * @return 
	 */
	public static List<String> extractMessageByRegular(String msg){
		
		List<String> list=new ArrayList<String>();
		Pattern p = Pattern.compile("(\\[[^\\]]*\\])");
		Matcher m = p.matcher(msg);
		while(m.find()){
			list.add(m.group().substring(1, m.group().length()-1));
		}
		return list;
	}
 
}
